package com.daigoudao.web.service;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.daigoudao.web.model.AreaModel;
import com.daigoudao.web.model.CanBuyModel;
import com.daigoudao.web.model.CategoryModel;
import com.daigoudao.web.model.UserModel;
import com.daigoudao.web.model.WantBuyModel;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

public class CacheService {
	private static Logger logger = LoggerFactory.getLogger(CacheService.class);
	
	private ICanBuyService canBuyService;
	private IWantBuyService wantBuyService;
	private IUserService userService;
	private ICommonService commonService;
	
	private Cache cache;

	public ICanBuyService getCanBuyService() {
		return canBuyService;
	}

	public void setCanBuyService(ICanBuyService canBuyService) {
		this.canBuyService = canBuyService;
	}

	public IWantBuyService getWantBuyService() {
		return wantBuyService;
	}

	public void setWantBuyService(IWantBuyService wantBuyService) {
		this.wantBuyService = wantBuyService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public ICommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	public Cache getCache() {
		return cache;
	}

	public void setCache(Cache cache) {
		this.cache = cache;
	}
	
	public void refresh(){
	
		synchronized(this){
			if(logger.isInfoEnabled()){
				logger.info("start init cache...");
			}
		
			try {
				UserModel userModel = new UserModel();
				userModel.setPageSize(7);
				
				List<CanBuyModel> canBuyList = canBuyService.getHotCanBuyList();
				List<WantBuyModel> wantBuyList = wantBuyService.getNewWantBuyList();
				List<UserModel> hotBrokerList = userService.getHotBrokerList(userModel);
				List<AreaModel> areaList = commonService.queryAreas();
				List<CategoryModel> cateList = commonService.queryCategorys();
				
				Element element = new Element("canBuyList", canBuyList);
				cache.put(element);
				
				element = new Element("wantBuyList", wantBuyList);
				cache.put(element);
				
				element = new Element("hotBrokerList", hotBrokerList);
				cache.put(element);
				
				element = new Element("areaList", areaList);
				cache.put(element);
				
				element = new Element("cateList", cateList);
				cache.put(element);
				
			} catch (Exception e) {
				if(logger.isErrorEnabled()){
					logger.error("init cache error", e);
				}
			}
			
			if(logger.isInfoEnabled()){
				logger.info("end init cache...");
			}
		}
	}

	@SuppressWarnings("unchecked")
	public List<CanBuyModel> getHotCanBuyList(){
		Element ele = cache.get("canBuyList");
		if(ele==null || ele.getValue()==null){
			refresh();
		}
		
		return (List<CanBuyModel>)ele.getValue();
	}
	
	@SuppressWarnings("unchecked")
	public List<WantBuyModel> getNewWantBuyList(){
		Element ele = cache.get("wantBuyList");
		if(ele==null || ele.getValue()==null){
			refresh();
		}
		
		return (List<WantBuyModel>)ele.getValue();
	}
	
	@SuppressWarnings("unchecked")
	public List<UserModel> getHotBrokerList(){
		Element ele = cache.get("hotBrokerList");
		if(ele==null || ele.getValue()==null){
			refresh();
		}
		
		return (List<UserModel>)ele.getValue();
	}
	
	@SuppressWarnings("unchecked")
	public List<AreaModel> getAreaList(){
		Element ele = cache.get("areaList");
		if(ele==null || ele.getValue()==null){
			refresh();
		}
		
		return (List<AreaModel>)ele.getValue();
	}
	
	@SuppressWarnings("unchecked")
	public List<CategoryModel> getCateList(){
		Element ele = cache.get("cateList");
		if(ele==null || ele.getValue()==null){
			refresh();
		}
		
		return (List<CategoryModel>)ele.getValue();
	}
}
